package uk.co.lukeparker.listeners;

import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;

import uk.co.lukeparker.JazzCraft;

public class PlayerListener implements Listener{
	
	private final JazzCraft plugin;
	
	public PlayerListener(JazzCraft plugin){
		this.plugin = plugin;
	}
	
	
	@EventHandler
	public void onPlayerJoin(PlayerJoinEvent event){
		Player player = event.getPlayer();
		FileConfiguration config = plugin.getConfig();
		event.setJoinMessage(ChatColor.GREEN + player.getName() + " joined the game!");
		if(config.getInt("Lobby Spawn.x") != 0 || config.getInt("Lobby Spawn.y") != 0 || config.getInt("Lobby Spawn.z") != 0){
			player.teleport(new Location(plugin.getServer().getWorld(config.getString("Lobby Spawn.world")), config.getInt("Lobby Spawn.x"), config.getInt("Lobby Spawn.y"), config.getInt("Lobby Spawn.z")));
		}else{
			plugin.getLogger().info("You need to set Lobby Spawn!");
		}
	}
	
	@EventHandler
	public void onPlayerLeave(PlayerQuitEvent event){
		Player player = event.getPlayer();
		event.setQuitMessage(ChatColor.DARK_RED + player.getName() + " left the game!");
		
		if(JazzCraft.team1.contains(player.getName())){
			JazzCraft.team1.remove(player.getName());
		}
		
		if(JazzCraft.team2.contains(player.getName())){
			JazzCraft.team2.remove(player.getName());
		}
	}
	
}
